package com.mcflies.commons.commons;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.dom4j.*;

import com.mcflies.aotoPartsManager.dao.LeftMenuTreeConfigDAO;
import com.mcflies.aotoPartsManager.pojo.LeftMenuTreeConfig;
import com.mcflies.aotoPartsManager.util.AutoPartsConstantKey;
import com.mcflies.aotoPartsManager.util.AutoPartsDAOFactory;
import com.mcflies.commons.bean.AreaBean;
import com.mcflies.commons.util.XMLParseUtil;

/**
 * 公用方法类 CommonFunction.java
 * 
 * @describe：
 * @Copyright(c) 2012-2024 Study,mcfly
 * @author mcfly_jun
 */
public class CommonFunction {
	private static Logger log = Logger.getLogger(CommonFunction.class);

	/**
	 * @describe：从bankList.xml中加载所有的银行信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getAllBankInfo() {
		String filePath = CommonFunction.class.getClassLoader().getResource("bankList.xml").getPath();
		Element rootElement = XMLParseUtil.getRootElement(filePath);
		Map<String, Object> bankMap = new HashMap<String, Object>();
		List<Object> bankList = new ArrayList<Object>();
		if (null == rootElement) {
			return null;
		}
		for (Iterator i = rootElement.elementIterator("bank"); i.hasNext();) {
			Map<String, Object> map = new HashMap<String, Object>();
			Element bank = (Element) i.next();
			String bankId = bank.elementText("bankId").trim();
			String bankName = bank.elementText("bankName").trim();
			map.put(CommonsConstantKey.McfliesDSConstant.bankId, bankId);
			map.put(CommonsConstantKey.McfliesDSConstant.bankName, bankName);
			bankList.add(map);
			log.info("bankId::" + bankId + "::bankName::" + bankName);
		}
		bankMap.put(CommonsConstantKey.McfliesDSConstant.root, bankList);
		return bankMap;
	}

	/**
	 * @describe：从areaList.xml中加载所有的省份和地市信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static AreaBean getAreaInfo() {
		String filePath = CommonFunction.class.getClassLoader().getResource("areaList.xml").getPath();
		Element rootElement = XMLParseUtil.getRootElement(filePath);
		if (null == rootElement) {
			return null;
		}

		List<Object> proviceList = new ArrayList<Object>();
		Map<String, Object> proviceMap = new HashMap<String, Object>();// 存放省份的映射关系
		Map<String, Object> proviceRegionMap = new HashMap<String, Object>();// 存放省份和地市的映射关系
		AreaBean areaBean = new AreaBean();
		for (Iterator rootIte = rootElement.elementIterator("province"); rootIte.hasNext();) {
			Element provinceEle = (Element) rootIte.next();// 省份子元素
			Map<String, Object> provinceMap_Temp = new HashMap<String, Object>();// 存放省份信息,临时
			// 迭代省份属性
			String provinceName = "";
			for (Iterator attrIte = provinceEle.attributeIterator(); attrIte.hasNext();) {
				Attribute attribute = (Attribute) attrIte.next();
				provinceName = attribute.getValue().trim();
				provinceMap_Temp.put(CommonsConstantKey.McfliesDSConstant.provinceName, provinceName);
				log.info("provinceName::::" + provinceName);
			}
			proviceList.add(provinceMap_Temp);

			List<Object> regionList_Temp = new ArrayList<Object>();
			// 迭代省份子元素(地市信息)
			for (Iterator eleIte = provinceEle.elementIterator(); eleIte.hasNext();) {
				Map<String, Object> regionMap_Temp = new HashMap<String, Object>();
				Element regionEle = (Element) eleIte.next();
				String regionName = regionEle.getText().trim();
				regionMap_Temp.put(CommonsConstantKey.McfliesDSConstant.regionName, regionName);
				log.info("regionName~~~~" + regionName);
				regionList_Temp.add(regionMap_Temp);
			}
			proviceRegionMap.put(provinceName, regionList_Temp);
		}
		proviceMap.put(CommonsConstantKey.McfliesDSConstant.root, proviceList);// 省份下拉数据源Map
		areaBean.setProvinceMap(proviceMap);
		areaBean.setProvinceRegionMap(proviceRegionMap);
		return areaBean;
	}

	/**
	 * @describe：加载左侧菜单树,并组成ExtJS Tree所需要的JSON数据格式
	 * @return
	 * @throws Exception
	 */
	public static JSONArray getLeftMenuTree() throws Exception {
		JSONArray jsonArr = new JSONArray();
		LeftMenuTreeConfigDAO configDAO = AutoPartsDAOFactory.getLeftMenuTreeConfigDAO();
		List<LeftMenuTreeConfig> list = configDAO.loadLeftMenuTreeConfig();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> rootMap = new HashMap<String, Object>();
			LeftMenuTreeConfig rootConfig = list.get(i);
			if (!AutoPartsConstantKey.LEFT_MENU_ROOT.equals(rootConfig.getParentMenuId())) {// 如果当前节点不是跟节点,继续循环
				continue;
			}
			rootMap.put(AutoPartsConstantKey.TREE_PANEL_ID, rootConfig.getMenuPanelId());
			rootMap.put(AutoPartsConstantKey.TREE_PANEL_TEXT, rootConfig.getMenuName());
			rootMap.put(AutoPartsConstantKey.TREE_PANEL_ICONCLS, rootConfig.getMenuIconCls());
			rootMap.put(AutoPartsConstantKey.TREE_PANEL_EXPANDED, String.valueOf(false));// 是否展开,默认关闭
			// 目前支配知道二级节点,找二级子节点
			List<Object> childList = new ArrayList<Object>();
			for (int j = 0; j < list.size(); j++) {
				LeftMenuTreeConfig secondConfig = list.get(j);
				// 二级节点的父节点为根节点,并且是配置是叶子节点(叶子节点为0)
				if (rootConfig.getMenuId().equals(secondConfig.getParentMenuId())
						&& AutoPartsConstantKey.LEFT_MENU_LEAF.equals(String.valueOf(secondConfig.getIsLeaf()))) {
					Map<String, Object> secondMap = new HashMap<String, Object>();
					secondMap.put(AutoPartsConstantKey.TREE_PANEL_ID, secondConfig.getMenuPanelId());
					secondMap.put(AutoPartsConstantKey.TREE_PANEL_TEXT, secondConfig.getMenuName());
					secondMap.put(AutoPartsConstantKey.TREE_PANEL_ICONCLS, secondConfig.getMenuIconCls());
					secondMap.put(AutoPartsConstantKey.TREE_PANEL_LEAF, String.valueOf(true));
					childList.add(secondMap);
				}
			}
			rootMap.put(AutoPartsConstantKey.TREE_PANEL_CHILDREN, childList);// 是否展开,默认关闭
			jsonArr.add(rootMap);
		}
		return jsonArr;
	}

	/**
	 * @describe：进行增,删,改,成功时,所需要返回的JSONObject
	 * @param retMap
	 * @return
	 */
	public static JSONObject showSuccessInfo(Map<String, Object> retMap) {
		JSONObject jsonObj = new JSONObject();
		retMap.put(CommonsConstantKey.EXTJS_RETURN_RESULT, CommonsConstantKey.EXTJS_RETURN_RESULT_TRUE);
		jsonObj = JSONObject.fromObject(retMap);
		return jsonObj;
	}

	/**
	 * @describe：进行增,删,改,异常时,所需要返回的JSONObject
	 * @param retMap
	 * @param e
	 * @return
	 */
	public static JSONObject showErrorInfo(Map<String, Object> retMap, Exception e) {
		JSONObject jsonObj = new JSONObject();
		retMap.put(CommonsConstantKey.EXTJS_RETURN_RESULT, CommonsConstantKey.EXTJS_RETURN_RESULT_FALSE);
		retMap.put(CommonsConstantKey.EXTJS_RETURN_EXCEPTION, e.getMessage());
		jsonObj = JSONObject.fromObject(retMap);
		return jsonObj;
	}

}
